import { Component } from "@angular/core";
import { SFSchema, SFUISchema } from "@delon/form";
import { _HttpClient } from "@delon/theme";
import { NzMessageService } from "ng-zorro-antd/message";
import { NzModalRef } from "ng-zorro-antd/modal";
import { Api } from "src/app/shared/api";
import { ResponseCode } from "src/app/shared/response.code";

@Component({
    selector: 'app-user-group',
    templateUrl: './group.component.html',
})
export class GroupComponent{
    record: any = {};
    schema: SFSchema = {
        properties: {
            name: {
                type: 'string',
                title: '名称',
            }
        },
    };
    ui: SFUISchema = {
        '*': {
            spanLabel: 2,
            spanControl: 22
        },
    };

    constructor(
    private modal: NzModalRef,
    private msgSrv: NzMessageService,
    public http: _HttpClient,
    ) {}

    close() {
        this.modal.destroy();
    }

    save(value: any) {
        console.log(value);
        if (this.record.id) {
            this.http.put(Api.BaseGroupApi, value).subscribe((res: any) => {
              if (res) {
                if (res.code === ResponseCode.SUCCESS) {
                  this.msgSrv.success('修改成功');
                  this.modal.close(true);
                } else {
                  this.msgSrv.warning(res.message);
                }
              } else {
                this.msgSrv.error('修改失败，未知错误');
              }
            });
        } else {
            this.http.post(Api.BaseGroupApi, value).subscribe((res: any) => {
                if (res) {
                if (res.code === ResponseCode.SUCCESS) {
                    this.msgSrv.success('保存成功');
                    this.modal.close(true);
                } else {
                    this.msgSrv.warning(res.message);
                }
                } else {
                this.msgSrv.error('保存失败，未知错误');
                }
            });
        }
    }
}